<template>
	<view class="tongji-container">
		<view class="form-section">
			<view class="form-item">
				<text class="form-label">用户姓名</text>
				<input class="form-input" v-model="form.userName" placeholder="请输入用户姓名" />
			</view>
			<view class="form-item">
				<text class="form-label">工号</text>
				<input class="form-input" v-model="form.employeeId" placeholder="请输入工号" />
				<button class="search-btn" @click="searchUserByEmployeeId" :loading="searching">查询</button>
			</view>
			<view class="form-item">
				<text class="form-label">增加业绩数值</text>
				<input class="form-input" v-model="form.addPerformanceValue" type="number" placeholder="请输入业绩数值" />
			</view>
			<view class="form-item">
				<text class="form-label">减少业绩数值</text>
				<input class="form-input" v-model="form.reducePerformanceValue" type="number" placeholder="请输入业绩数值" />
			</view>
			<!-- <view class="form-item">
				<text class="form-label">特饮内容</text>
				<input class="form-input" v-model="form.specialDrink" placeholder="请输入特饮内容" />
			</view> -->
			<view class="form-item">
				<text class="form-label">增加奖励值</text>
				<input class="form-input" v-model="form.addRewardValue" type="number" placeholder="请输入奖励值" />
			</view>
			<view class="form-item">
				<text class="form-label">减少奖励值</text>
				<input class="form-input" v-model="form.reduceRewardValue" type="number" placeholder="请输入奖励值" />
			</view>
			<view class="form-item">
				<text class="form-label">房间号</text>
				<input class="form-input" v-model="form.cc" placeholder="请输入房间号" />
			</view>
			<view class="form-item">
				<text class="form-label required">日期</text>
				<picker mode="date" :value="form.aa" @change="handleDateChange" class="date-picker">
					<view class="picker-text" :class="{ 'placeholder': !form.aa }">{{ form.aa || '请选择日期 *' }}</view>
				</picker>
			</view>
			<view class="form-buttons">
				<button class="form-btn" @click="handleSubmit">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
import { getUser, listUser } from '@/api/system/user'
import { addPerformanceyj } from '@/api/system/performanceyj'
export default {
	data() {
		return {
			form: {
				userName: '',
				employeeId: '',
				addPerformanceValue: '', // 增加业绩数值
				reducePerformanceValue: '', // 减少业绩数值
				specialDrink: '',
				addRewardValue: '', // 增加奖励值
				reduceRewardValue: '', // 减少奖励值
				cc: '', // 房间号
				aa: ''  // 日期
			},
			submitting: false,
			searching: false
		}
	},
	methods: {
		handleDateChange(e) {
			this.form.aa = e.detail.value
		},
		// 通过工号查询用户信息
		async searchUserByEmployeeId() {
			if (!this.form.employeeId) {
				uni.showToast({ title: '请输入工号', icon: 'none' })
				return
			}
			
			this.searching = true
			try {
				// 通过工号查询用户
				const userRes = await listUser({ employeeId: this.form.employeeId })
				if (!userRes || userRes.code !== 200 || !userRes.rows || userRes.rows.length === 0) {
					uni.showToast({ title: '未找到该工号对应的用户', icon: 'none' })
					return
				}
				
				const user = userRes.rows[0]
				// 自动填入用户名
				this.form.userName = user.userName
				uni.showToast({ title: '查询成功', icon: 'success' })
			} catch (e) {
				console.error('查询用户失败：', e)
				uni.showToast({ title: '查询失败', icon: 'none' })
			} finally {
				this.searching = false
			}
		},
		async handleSubmit() {
			if (!this.form.userName) {
				uni.showToast({ title: '请输入用户姓名', icon: 'none' })
				return
			}
			if (!this.form.aa) {
				uni.showToast({ title: '请选择日期', icon: 'none' })
				return
			}
			// 检查是否至少输入了一个业绩值或奖励值
			const hasPerformanceValue = this.form.addPerformanceValue || this.form.reducePerformanceValue
			const hasRewardValue = this.form.addRewardValue || this.form.reduceRewardValue
			if (!hasPerformanceValue && !hasRewardValue) {
				uni.showToast({ title: '请至少输入一个业绩值或奖励值', icon: 'none' })
				return
			}
			this.submitting = true
			try {
				// 先查用户ID
				const userRes = await listUser({ userName: this.form.userName })
				if (!userRes || userRes.code !== 200 || !userRes.rows || userRes.rows.length === 0) {
					uni.showToast({ title: '未找到该用户', icon: 'none' })
					this.submitting = false
					return
				}
				const user = userRes.rows[0]
				// 计算业绩值：增加为正数，减少为负数
				let performanceValue = 0
				if (this.form.addPerformanceValue) {
					performanceValue += parseFloat(this.form.addPerformanceValue) || 0
				}
				if (this.form.reducePerformanceValue) {
					performanceValue -= parseFloat(this.form.reducePerformanceValue) || 0
				}
				// 计算奖励值：增加为正数，减少为负数
				let rewardValue = 0
				if (this.form.addRewardValue) {
					rewardValue += parseFloat(this.form.addRewardValue) || 0
				}
				if (this.form.reduceRewardValue) {
					rewardValue -= parseFloat(this.form.reduceRewardValue) || 0
				}
				const params = {
					id: null,
					userId: user.userId,
					userName: user.userName,
					performanceValue: performanceValue,
					specialDrink: this.form.specialDrink,
					aa: this.form.aa, // 日期可为空
					bb: rewardValue, // 使用计算后的奖励值
					cc: this.form.cc // 房间号可为空
				}
				const res = await addPerformanceyj(params)
				if (res && res.code === 200) {
					uni.showToast({ title: '提交成功', icon: 'success' })
					// 清空表单
					this.form.userName = ''
					this.form.employeeId = ''
					this.form.addPerformanceValue = ''
					this.form.reducePerformanceValue = ''
					this.form.specialDrink = ''
					this.form.addRewardValue = ''
					this.form.reduceRewardValue = ''
					this.form.cc = ''
					this.form.aa = ''
				} else {
					uni.showToast({ title: res.msg || '提交失败', icon: 'none' })
				}
			} catch (e) {
				uni.showToast({ title: '提交异常', icon: 'none' })
			} finally {
				this.submitting = false
			}
		}
	}
}
</script>

<style scoped>
	.tongji-container {
		padding: 40rpx 24rpx;
	}
	.form-section {
		background: #fff;
		border-radius: 16rpx;
		padding: 32rpx 24rpx 24rpx 24rpx;
		box-shadow: 0 2rpx 12rpx #f0f1f5;
	}
	.form-item {
		display: flex;
		align-items: center;
		margin-bottom: 32rpx;
	}
	.form-label {
		min-width: 120rpx;
		font-size: 28rpx;
		color: #333;
	}
	.form-input {
		flex: 1;
		border: 1px solid #e5e5e5;
		border-radius: 8rpx;
		padding: 12rpx 20rpx;
		font-size: 28rpx;
		background: #f8f8f8;
		margin-left: 12rpx;
		height: 96rpx;
		line-height: 96rpx;
		box-sizing: border-box;
	}
	.search-btn {
		background: #28a745;
		color: #fff;
		border: none;
		border-radius: 8rpx;
		padding: 12rpx 24rpx;
		font-size: 26rpx;
		margin-left: 12rpx;
		height: 96rpx;
		line-height: 72rpx;
		box-sizing: border-box;
	}
	.form-buttons {
		display: flex;
		justify-content: flex-end;
	}
	.form-btn {
		background: #3478f6;
		color: #fff;
		border: none;
		border-radius: 8rpx;
		padding: 16rpx 60rpx;
		font-size: 30rpx;
	}
	.picker-text {
		flex: 1;
		border: 1px solid #e5e5e5;
		border-radius: 8rpx;
		padding: 12rpx 20rpx;
		font-size: 28rpx;
		background: #f8f8f8;
		margin-left: 12rpx;
		height: 96rpx;
		line-height: 96rpx;
		box-sizing: border-box;
	}
	
	.required {
		color: #e74c3c;
	}
	
	.required::after {
		content: ' *';
		color: #e74c3c;
	}
	
	.placeholder {
		color: #999;
	}
</style>
